<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <style>
        /* 基本的页面背景 */
        body {
            background-color: #f7f7f7;
            font-family: 'Arial', sans-serif;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            flex-direction: column;
        }

        /* 登录框 */
        .denglu {
            width: 400px;
            padding: 40px;
            background-color: #fff;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            text-align: center;
            transition: transform 0.3s ease-in-out;
        }

        .denglu:hover {
            transform: scale(1.02);
        }

        /* 页面标题 */
        .denglu h2 {
            color: #4CAF50;
            font-size: 26px;
            margin-bottom: 20px;
            font-weight: bold;
        }

        /* 输入框容器 */
        .input-group {
            margin-bottom: 20px;
            text-align: left;
        }

        /* 输入框标签 */
        .input-group label {
            display: block;
            font-weight: bold;
            font-size: 14px;
            color: #555;
            margin-bottom: 8px;
        }

        /* 输入框样式 */
        .input-group input {
            width: 100%;
            padding: 14px;
            font-size: 16px;
            border: 2px solid #ddd;
            border-radius: 8px;
            box-sizing: border-box;
            transition: all 0.3s ease;
            margin-top: 5px;
            background-color: #f9f9f9;
        }

        .input-group input:focus {
            border-color: #4CAF50;
            outline: none;
            background-color: #fff;
        }

        /* 按钮样式 */
        .submit_group input {
            width: 100%;
            padding: 14px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s, transform 0.3s ease;
        }

        .submit_group input:hover {
            background-color: #45a049;
            transform: translateY(-2px);
        }

        .submit_group input:active {
            transform: translateY(2px);
        }

        /* 错误提示信息 */
        #output, #output1 {
            color: red;
            margin-top: 10px;
            font-size: 14px;
            display: none; /* 初始隐藏 */
        }

        /* 成功提示信息 */
        #output.success {
            color: #4CAF50;
        }

        /* 页面底部的链接 */
        .footer-links {
            margin-top: 20px;
            text-align: center;
            font-size: 14px;
            color: #777;
        }

        .footer-links a {
            color: #4CAF50;
            text-decoration: none;
            margin: 0 10px;
        }

        .footer-links a:hover {
            text-decoration: underline;
        }

        /* 边框和背景 */
        .footer-links {
            border-top: 1px solid #ddd;
            padding-top: 10px;
        }

        /* 响应式设计 */
        @media (max-width: 500px) {
            .denglu {
                width: 90%;
                padding: 20px;
            }

            .denglu h2 {
                font-size: 22px;
            }

            .input-group input {
                font-size: 14px;
                padding: 12px;
            }

            .submit_group input {
                font-size: 14px;
                padding: 12px;
            }
        }
    </style>
</head>

<body>
    <div class="denglu">
        <h2>注册</h2>
        <form id="dataForm">
            <div class="input-group">
                <label for="email">邮箱 (请使用国内邮箱)</label>
                <input type="text" id="email" name="email" placeholder="请输入您的邮箱">
            </div>

            <div class="input-group">
                <label for="username">账号</label>
                <input type="text" id="username" name="username" placeholder="请输入用户名">
            </div>

            <div class="input-group">
                <label for="inputCode">邮箱验证码</label>
                <input type="text" id="inputCode" name="inputCode" placeholder="请输入验证码">
            </div>

            <div class="input-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入密码">
            </div>

            <div class="submit_group">
                <input type="submit" value="注册" name="register" id="register">
            </div>
        </form>
    </div>

    <!-- 提示消息显示区域 -->
    <div id="messageBox" style="display: none; text-align: center; margin-top: 20px;">
        <pre id="output"></pre>
        <pre id="output1"></pre>
    </div>

    <script>
        const registerButton = document.getElementById('register');
        registerButton.addEventListener('click', async function (event) {
            event.preventDefault();  // 阻止表单默认提交

            // 清除先前的错误信息
            document.getElementById('output').style.display = 'none';
            document.getElementById('output1').style.display = 'none';

            // 获取表单数据
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const email = document.getElementById('email').value;
            const inputCode = document.getElementById('inputCode').value;

            // 简单的表单验证
            if (!email || !username || !password || !inputCode) {
                document.getElementById("output").style.display = 'block';
                document.getElementById("output").textContent = "邮箱、用户名、密码和验证码不能为空";
                document.getElementById("messageBox").style.display = 'block';
                return;
            }

            // 邮箱格式验证
            const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
            if (!emailRegex.test(email)) {
                document.getElementById("output").style.display = 'block';
                document.getElementById("output").textContent = "请输入有效的邮箱地址";
                document.getElementById("messageBox").style.display = 'block';
                return;
            }

            // 密码强度验证
            if (password.length < 6) {
                document.getElementById("output").style.display = 'block';
                document.getElementById("output").textContent = "密码长度至少为6位";
                document.getElementById("messageBox").style.display = 'block';
                return;
            }

            try {
                // 发送 POST 请求调用后端的插入数据 API
                const response = await fetch("http://124.221.110.118:3000/insertData_zhanghao", {
                    method: "POST",
                    headers: {
                        "Content-Type": "application/json"
                    },
                    body: JSON.stringify({ username, password, email, inputCode })
                });

                const result = await response.json();

                if (response.status === 200) {
                    // 注册成功，跳转到登录页面
                    document.getElementById("output1").style.display = 'block';
                    document.getElementById("output1").textContent = "验证码正确";
                    document.getElementById("output").style.display = 'block';
                    document.getElementById("output").textContent = "注册成功！3秒后跳转登录界面";
                    setTimeout(() => {
                        window.location.href = "./login.html";  // 跳转到登录页面
                    }, 3000);
                } else if (response.status === 202) {
                    document.getElementById("output1").style.display = 'block';
                    document.getElementById("output1").textContent = "验证码错误";
                    document.getElementById("messageBox").style.display = 'block';
                } else if (response.status === 201) {
                    document.getElementById("output").style.display = 'block';
                    document.getElementById("output").textContent = "注册失败, 用户名或邮箱已被使用！";
                    document.getElementById("messageBox").style.display = 'block';
                } else {
                    // 其他错误
                    document.getElementById("output").style.display = 'block';
                    document.getElementById("output").textContent = "注册失败，请稍后再试！";
                    document.getElementById("messageBox").style.display = 'block';
                }
            } catch (error) {
                // 网络错误或其他未知错误
                document.getElementById("output").style.display = 'block';
                document.getElementById("output").textContent = "网络错误，注册失败！";
                document.getElementById("messageBox").style.display = 'block';
            }
        });
    </script>
</body>

</html>
